import React from 'react'
import { Route, Redirect, Switch } from 'react-router-dom'
const renderRoutes = (routes, isLogin, authPath = '/login', indexPath, switchProps = {}) => routes ? (
    <Switch  {...switchProps}>
        {routes.map((route, i) => (
            <Route
                key={route.key || i}
                path={route.path}
                exact={route.exact}
                strict={route.strict}
                render={(props) => {
                    document.title = route.title || "网页标题"
                    if (!route.requiresAuth || isLogin) {
                        if (route.path === '/') {
                            // 跳转默认页
                            return <Redirect to={{ pathname: indexPath }} />
                        } else {
                            return <route.component {...props} />
                        }
                    } else {
                        return <Redirect to={{ pathname: authPath, state: { from: props.location } }} />
                    }
                }}
            />
        ))}
        <Redirect to="/404" />
    </Switch>
) : null

export default renderRoutes